<?php
namespace app\home\controller;

class Community extends \think\Controller
{
	public function index()
	{
		$user_id = session('user_id');
		if( $user_id != NULL)
		{
			$result = db('user')->where("user_id={$user_id}")->find();
			$this->assign('user_id',$user_id);
			$this->assign('result',$result);
		}
		else //==NULL
		{
			$user_id = "";
			$result="";
    		$result['user_name']="普通用户";
    		$this->assign("result",$result);
			$this->assign('user_id',$user_id);
		}
		$ret = db('note')->alias('a')->join('user b','a.user_id=b.user_id')->where("note_status=2")->field('a.*,b.user_name')->order('addtime desc')->paginate(4);
		//note_status=1表示推荐，默认为0
		$label = db('note')->field('note_label')->select();
		//$data = implode("",$data);
    	foreach( $ret as $val )
    	{
    		$label = $val['note_label'];
			$label = explode(",",$label);
			$label = array_filter($label); //去空白的标签数组
			//dump($label);
			$val['note_label'] = $label;
    		$data[] = $val;
    	}
    	$this->assign('ret',$data);//显示detail内容
    	$pageHtml = $ret->render();
		$this->assign('pageHtml',$pageHtml);
		return view('index');
	}

	public function allnity()
	{
		$user_id = session('user_id');
		if( $user_id != NULL)
		{
			$result = db('user')->where("user_id={$user_id}")->find();
			$ret  = db("note")->alias('n')->join('user u','n.user_id=u.user_id')->field('n.*,u.user_name')->limit(5)->select();
			$ret1 = db("note")->alias('n')->join('user u','n.user_id=u.user_id')->field('n.*,u.user_name')->limit(4)->select();
			$ret2 = db("note")->alias('n')->join('user u','n.user_id=u.user_id')->field('n.*,u.user_name')->where("note_label='经验之谈' and note_status!=0")->select();
			$ret3 = db("note")->alias('n')->join('user u','n.user_id=u.user_id')->field('n.*,u.user_name,u.user_img')
			->where("note_label='二手市场' and note_status!=0")->select();
			$ret4 = db("note")->alias('n')->join('user u','n.user_id=u.user_id')->field('n.*,u.user_name')->where("note_label='休闲' and note_status!=0")->select();
			$ret5 = db("note")->alias('n')->join('user u','n.user_id=u.user_id')->field('n.*,u.user_name')->where("note_label='颓废' and note_status!=0")->select();
			$ret6 = db('user')->limit(0,5)->select();
			$ret7 = db("note")->alias('n')->join('user u','n.user_id=u.user_id')->field('n.*,u.user_name')->where("note_label='知识领域' and note_status!=0")->select();
			$this->assign("ret",$ret);
    		$this->assign("ret1",$ret1);
    		$this->assign("ret2",$ret2);
    		$this->assign("ret3",$ret3);
    		$this->assign("ret4",$ret4);
    		$this->assign("ret5",$ret5);
    		$this->assign("ret6",$ret6);
    		$this->assign("ret7",$ret7);
			$this->assign('user_id',$user_id);
			$this->assign('result',$result);
		}
		else
		{
			$user_id = "";
			$result="";
    		$result['user_name']="普通用户";
    		$ret = db("note")->alias('n')->join('user u','n.user_id=u.user_id')->field('n.*,u.user_name')->limit(5)->select();
			$ret1 = db("note")->alias('n')->join('user u','n.user_id=u.user_id')->field('n.*,u.user_name')->limit(4)->select();
    		$ret2 = db("note")->alias('n')->join('user u','n.user_id=u.user_id')->field('n.*,u.user_name')->where("note_label='经验之谈'")->select();
			$ret3 = db("note")->alias('n')->join('user u','n.user_id=u.user_id')->field('n.*,u.user_name,u.user_img')->where("note_label='二手市场'")->select();
			$ret4 = db("note")->alias('n')->join('user u','n.user_id=u.user_id')->field('n.*,u.user_name')->where("note_label='休闲'")->select();
			$ret5 = db("note")->alias('n')->join('user u','n.user_id=u.user_id')->field('n.*,u.user_name')->where("note_label='颓废'")->select();
			$ret6 = db('user')->select();
			$ret7 = db("note")->alias('n')->join('user u','n.user_id=u.user_id')->field('n.*,u.user_name')->where("note_label='知识领域'")->select();
    		$this->assign("ret",$ret);
    		$this->assign("ret1",$ret1);
    		$this->assign("ret2",$ret2);
    		$this->assign("ret3",$ret3);
    		$this->assign("ret4",$ret4);
    		$this->assign("ret5",$ret5);
    		$this->assign("ret6",$ret6);
    		$this->assign("ret7",$ret7);
    		$this->assign("result",$result);
			$this->assign('user_id',$user_id);
		}

		return view('allnity');
	}
}